<template>
  <div
      :style='{"width":"1200px","padding":"120px 7% 100px","margin":"0px auto","position":"relative","background":"url(http://codegen.caihongy.cn/20230109/138a4d606eae4a168810cef229969885.png) no-repeat center top,url(http://codegen.caihongy.cn/20230109/b95aeae1c1294f398aefb7c6cd5b16de.png) no-repeat center top,url(http://codegen.caihongy.cn/20230109/5ffc803e6682418eb7f0b09a98e35527.png) no-repeat center bottom,#99CC99"}'>
    <el-form
        class="add-update-preview"
        ref="ruleForm"
        :model="ruleForm"
        :rules="rules"
        label-width="150px"
    >
      <el-form-item :style='{"padding":"10px","boxShadow":"0 2px 0 #efe4d4","margin":"0 0 10px"}' label="头像"
                    v-if="type!='cross' || (type=='cross' && !ro.avatarurl)" prop="avatarurl">
        <file-upload
            tip="点击上传头像"
            action="file/upload"
            :limit="3"
            :multiple="true"
            :fileUrls="ruleForm.avatarurl?ruleForm.avatarurl:''"
            @change="avatarurlUploadChange"
        ></file-upload>
      </el-form-item>
      <el-form-item :style='{"padding":"10px","boxShadow":"0 2px 0 #efe4d4","margin":"0 0 10px"}' class="upload" v-else
                    label="头像" prop="avatarurl">
        <img v-if="ruleForm.avatarurl.substring(0,4)=='http'" class="upload-img" style="margin-right:20px;"
             v-bind:key="index" :src="ruleForm.avatarurl.split(',')[0]" width="100" height="100">
        <img v-else class="upload-img" style="margin-right:20px;" v-bind:key="index"
             v-for="(item,index) in ruleForm.avatarurl.split(',')" :src="baseUrl+item" width="100" height="100">
      </el-form-item>
      <el-form-item :style='{"padding":"10px","boxShadow":"0 2px 0 #efe4d4","margin":"0 0 10px"}' label="用户名"
                    prop="nickname">
        <el-input v-model="ruleForm.nickname"
                  placeholder="用户名" clearable></el-input>
      </el-form-item>
      <el-form-item :style='{"padding":"10px","boxShadow":"0 2px 0 #efe4d4","margin":"0 0 10px"}' label="评论内容"
                    prop="content">
        <el-input
            type="textarea"
            :rows="8"
            placeholder="评论内容"
            v-model="ruleForm.content">
        </el-input>
      </el-form-item>
      <el-form-item :style='{"padding":"10px","boxShadow":"0 2px 0 #efe4d4","margin":"0 0 10px"}' label="回复内容"
                    prop="reply">
        <el-input
            type="textarea"
            :rows="8"
            placeholder="回复内容"
            v-model="ruleForm.reply">
        </el-input>
      </el-form-item>

      <el-form-item :style='{"padding":"0","textAlign":"center","margin":"0"}'>
        <el-button
            :style='{"border":"0","cursor":"pointer","padding":"0","margin":"0 20px 0 0","outline":"none","color":"rgba(255, 255, 255, 1)","borderRadius":"4px","background":"#ff9966","width":"128px","lineHeight":"40px","fontSize":"14px","height":"40px"}'
            type="primary" @click="onSubmit">提交
        </el-button>
        <el-button
            :style='{"border":"0px solid rgba(64, 158, 255, 1)","cursor":"pointer","padding":"0","margin":"0","outline":"none","color":"#fff","borderRadius":"4px","background":"#009688","width":"128px","lineHeight":"40px","fontSize":"14px","height":"40px"}'
            @click="back()">返回
        </el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      id: '',
      baseUrl: '',
      ro: {
        refid: false,
        userid: false,
        avatarurl: false,
        nickname: false,
        content: false,
        reply: false,
      },
      type: '',
      userTableName: localStorage.getItem('UserTableName'),
      ruleForm: {
        refid: '',
        userid: '',
        avatarurl: '',
        nickname: '',
        content: '',
        reply: '',
      },
      rules: {
        refid: [
          {required: true, message: '关联表id不能为空', trigger: 'blur'},
        ],
        userid: [
          {required: true, message: '用户id不能为空', trigger: 'blur'},
        ],
        avatarurl: [],
        nickname: [],
        content: [
          {required: true, message: '评论内容不能为空', trigger: 'blur'},
        ],
        reply: [],
      },
    };
  },
  computed: {},
  components: {},
  created() {
    //this.bg();
    let type = this.$route.query.type ? this.$route.query.type : '';
    this.init(type);
    this.baseUrl = this.$config.baseUrl;
  },
  methods: {
    getMakeZero(s) {
      return s < 10 ? '0' + s : s;
    },
    // 下载
    download(file) {
      window.open(`${file}`)
    },
    // 初始化
    init(type) {
      this.type = type;
      if (type == 'cross') {
        var obj = JSON.parse(localStorage.getItem('crossObj'));
        for (var o in obj) {
          if (o == 'refid') {
            this.ruleForm.refid = obj[o];
            this.ro.refid = true;
            continue;
          }
          if (o == 'userid') {
            this.ruleForm.userid = obj[o];
            this.ro.userid = true;
            continue;
          }
          if (o == 'avatarurl') {
            this.ruleForm.avatarurl = obj[o].split(",")[0];
            this.ro.avatarurl = true;
            continue;
          }
          if (o == 'nickname') {
            this.ruleForm.nickname = obj[o];
            this.ro.nickname = true;
            continue;
          }
          if (o == 'content') {
            this.ruleForm.content = obj[o];
            this.ro.content = true;
            continue;
          }
          if (o == 'reply') {
            this.ruleForm.reply = obj[o];
            this.ro.reply = true;
            continue;
          }
        }
      }
    },

    // 多级联动参数
    // 多级联动参数
    info(id) {
      this.$http.get('discussjingcaishipin/detail/${id}', {emulateJSON: true}).then(res => {
        if (res.data.code == 0) {
          this.ruleForm = res.data.data;
        }
      });
    },
    // 提交
    onSubmit() {

      //更新跨表属性
      var crossuserid;
      var crossrefid;
      var crossoptnum;
      this.$refs["ruleForm"].validate(valid => {
        if (valid) {
          if (this.type == 'cross') {
            var statusColumnName = localStorage.getItem('statusColumnName');
            var statusColumnValue = localStorage.getItem('statusColumnValue');
            if (statusColumnName && statusColumnName != '') {
              var obj = JSON.parse(localStorage.getItem('crossObj'));
              if (!statusColumnName.startsWith("[")) {
                for (var o in obj) {
                  if (o == statusColumnName) {
                    obj[o] = statusColumnValue;
                  }
                }
                var table = localStorage.getItem('crossTable');
                this.$http.post(table + '/update', obj).then(res => {
                });
              } else {
                crossuserid = Number(localStorage.getItem('userid'));
                crossrefid = obj['id'];
                crossoptnum = localStorage.getItem('statusColumnName');
                crossoptnum = crossoptnum.replace(/\[/, "").replace(/\]/, "");
              }
            }
          }
          if (crossrefid && crossuserid) {
            this.ruleForm.crossuserid = crossuserid;
            this.ruleForm.crossrefid = crossrefid;
            var params = {
              page: 1,
              limit: 10,
              crossuserid: crossuserid,
              crossrefid: crossrefid,
            }
            this.$http.get('discussjingcaishipin/list', {
              params: params
            }).then(res => {
              if (res.data.data.total >= crossoptnum) {
                this.$message({
                  message: localStorage.getItem('tips'),
                  type: 'success',
                  duration: 1500,
                });
                return false;
              } else {
                // 跨表计算


                this.$http.post('discussjingcaishipin/add', this.ruleForm).then(res => {
                  if (res.data.code == 0) {
                    this.$message({
                      message: '操作成功',
                      type: 'success',
                      duration: 1500,
                      onClose: () => {
                        this.$router.go(-1);
                      }
                    });
                  } else {
                    this.$message({
                      message: res.data.msg,
                      type: 'error',
                      duration: 1500
                    });
                  }
                });
              }
            });
          } else {


            this.$http.post('discussjingcaishipin/add', this.ruleForm).then(res => {
              if (res.data.code == 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.$router.go(-1);
                  }
                });
              } else {
                this.$message({
                  message: res.data.msg,
                  type: 'error',
                  duration: 1500
                });
              }
            });
          }
        }
      });
    },
    // 获取uuid
    getUUID() {
      return new Date().getTime();
    },
    // 返回
    back() {
      this.$router.go(-1);
    },
    avatarurlUploadChange(fileUrls) {
      this.ruleForm.avatarurl = fileUrls.replace(new RegExp(this.$config.baseUrl, "g"), "");
      ;
    },
  }
};
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.el-date-editor.el-input {
  width: auto;
}

.add-update-preview .el-form-item ::v-deep .el-form-item__label {
  padding: 0 10px 0 0;
  color: #666;
  font-weight: 500;
  width: 150px;
  font-size: 14px;
  line-height: 40px;
  text-align: right;
}

.add-update-preview .el-form-item ::v-deep .el-form-item__content {
  margin-left: 150px;
}

.add-update-preview .el-input ::v-deep .el-input__inner {
  border: 1px solid #e5d9c6;
  border-radius: 4px;
  padding: 0 12px;
  outline: none;
  color: #333;
  width: 400px;
  font-size: 14px;
  height: 40px;
}

.add-update-preview .el-select ::v-deep .el-input__inner {
  border: 1px solid #e5d9c6;
  border-radius: 4px;
  padding: 0 10px;
  outline: none;
  color: #333;
  width: 200px;
  font-size: 14px;
  height: 40px;
}

.add-update-preview .el-date-editor ::v-deep .el-input__inner {
  border: 1px solid #e5d9c6;
  border-radius: 4px;
  padding: 0 10px 0 30px;
  outline: none;
  color: #333;
  width: 200px;
  font-size: 14px;
  height: 40px;
}

.add-update-preview ::v-deep .el-upload--picture-card {
  background: transparent;
  border: 0;
  border-radius: 0;
  width: auto;
  height: auto;
  line-height: initial;
  vertical-align: middle;
}

.add-update-preview ::v-deep .upload .upload-img {
  border: 1px solid #e5d9c6;
  cursor: pointer;
  border-radius: 6px;
  color: #333;
  width: 100px;
  font-size: 32px;
  line-height: 100px;
  text-align: center;
  height: 100px;
}

.add-update-preview ::v-deep .el-upload-list .el-upload-list__item {
  border: 1px solid #e5d9c6;
  cursor: pointer;
  border-radius: 6px;
  color: #333;
  width: 100px;
  font-size: 32px;
  line-height: 100px;
  text-align: center;
  height: 100px;
}

.add-update-preview ::v-deep .el-upload .el-icon-plus {
  border: 1px solid #e5d9c6;
  cursor: pointer;
  border-radius: 6px;
  color: #333;
  width: 100px;
  font-size: 32px;
  line-height: 100px;
  text-align: center;
  height: 100px;
}

.add-update-preview .el-textarea ::v-deep .el-textarea__inner {
  border: 1px solid #e5d9c6;
  border-radius: 4px;
  padding: 12px;
  outline: none;
  color: #333;
  width: 400px;
  font-size: 14px;
  height: 120px;
}
</style>
